﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Searches
{
    class Program
    {
        static void Main(string[] args)
        {
            long sum = 0;
	        int[] a = new int[20000];
	        for(int i = 0 ; i < 20000; i++)
	        {
		        a[i] = i+1;
	        }

            Stopwatch stopwatch = Stopwatch.StartNew();

	        for (int b = 0; b < 9000; b++)
            {
                for (int z = 0; z < 9000; z++)
                {
                    sum += Search(a, z + b, 0, 19999);
                }
            }

            Console.WriteLine("Iterations took for {0} seconds", stopwatch.Elapsed.TotalSeconds);
	        Console.WriteLine("Sum : {0}", sum);
            Console.ReadKey();
        }

        public static int Search(int[] sortedArray, int v, int l, int r)
        {
            while (r >= 1)
            {
                int m = (l + r) / 2;
                if (v == sortedArray[m])
                    return m;
                if (v < sortedArray[m])
                    r = m - 1;
                else
                    l = m + 1;
            }

            return -1;
        }
    }
}
